var clipboard={
	dir:"",
	ids:[],
	isCopy:false,
	used:false
};
//被选择的文件
var selFileIds=[];

var fileTabBtsVue=new Vue({
    el:'#fileTabBts',
    data: {
        selectedCnt:0,
		clipboardUsed:false
    },
    methods:{
        addSelected:function () {
            this.selectedCnt++;
        },
        subSelected:function () {
            this.selectedCnt--;
        }


    }
})
//文件显示
var nowPath="/";//当前所在目录
var fileList=[{
    filename:'文件夹1',
    lastModified:'2021/08/23 12:33:46',
    type:'文件夹',
    size:''
},{
    filename:'测试文件.exe',
    lastModified:'2021/08/23 12:33:46',
    type:'可执行文件',
    size:'66KB'
}]
var fileVue=new Vue({
    el: '#fileManager',
    data: {
        fileList: fileList
    }
});
function showError(msg){
    console.log(msg)
}
loadList(nowPath)
/***************加载文件列表并显示**************/
function loadList(path){
    console.log("加载 ",path)
    console.log("加载 ",Base64.encodeURI(path))
	//情况剪贴板
	fileTabBtsVue.clipboardUsed = false;
	clipboard.used=false;
    $.ajax({
        type: "GET", //提交的方法
        url: "disk/list?path="+Base64.encodeURI(path), //提交的地址
        //data: $('#loginForm').serialize(), // 序列化表单值
        dataType:'json',
        async: false,
        error: function(request) {
            //失败提示
            console.log(request)
            showError("失败")
        },
        success: function(data) { //成功
            showError("加载成功");
            console.log(data);
            if (data.status===0) {
                fileVue.fileList=data.list;
                nowPath=path;
            }
            else showError("获取失败");
        }
    });
}
/***************发送请求**************/
function doFileCMD(isSingle,data,fail,success){
	$.ajax({
	    type: "POST", url: "disk/"+isSingle? "file":"files", 
	    data: data, dataType:'json',async: false,
		error: fail,success: success
	});
}
/******************选择文件******************/
function checkFileAllClick(checked){
    let li=document.getElementsByClassName('checkFileBox');
    if (checked) for (let i =0;i<li.length;++i) li[i].checked=true;
    else for (let i =0;i<li.length;++i) li[i].checked=false;
    fileTabBtsVue.selectedCnt=checked? li.length:0;
}
function checkFileClick(checked) {
    if (checked) fileTabBtsVue.addSelected();
    else fileTabBtsVue.subSelected();
}

/******************显示询问框******************/
function showFileDialog(title,confirm){
    console.log(selFileIds)
    let html='';
	for (let i in selFileIds)
		html+='<li>'+fileList[selFileIds[i]].filename+'</li>\n';
	console.log(html)
	fileOpDialogTitle.innerHTML=title;
	fileOpDialogFileList.innerHTML=html;
	fileOpDialogConfirmBt.onclick=confirm;
    $("#fileOpDialog").modal("show");
}
/******************共享文件******************/
//click
function sharedFileConfig(){
	let limit='';
	limit+=sharedCheckRead.checked? '1':'0';
	limit+=sharedCheckWrite.checked? '1':'0';
	limit+='-';
	limit+=sharedCheckUserRead.checked? '1':'0';
	limit+=sharedCheckUserWrite.checked? '1':'0';
	limit+='-';
	limit+=sharedCheckSearch.checked? '1':'0';
	limit+=sharedCheckUsePw.checked? '1':'0';
	console.log(data)
	
	let paths=[];
	for (let i in selFileIds)
		paths.push(clipboard.dir+"/"+fileVue.fileList[selFileIds[i]].filename)
	
	let data={
		path:(paths.length==1)? paths[0]:paths,
		limit:limit,
		password:sharedCheckPwInput.value,
		timeout:new Date().getTime()+30*12*3600*1000
	}
	
	//发送请求
	$.ajax({
	    type: "POST",url: "share/"+(paths.length==1)?"addShare":"addShares", 
	    data: data, dataType:'json',async: false,
	    error: function(error){
			//失败
			console.log(error)
		},
	    success: function(res){
			//成功
			console.log(res)
			// loadList(nowPath);
		}
	});
	
}
/******************重命名文件******************/
var renameFileConfirm = function (){
	let oldname=fileVue.fileList[selFileIds[0]].filename;
	let newname=inputDialogInput.value;
	
	if(oldname===newname) return;
	let data={
		action:"rename",
		source:nowPath+"/"+oldname,
		target:nowPath+"/"+newname
	}
	doFileCMD(true,data,function(error){
		//失败
		console.log(error)
	},function(res){
		//成功
		console.log(res)
		loadList(nowPath);
	});
}
/******************新建文件夹******************/
var mkdirConfirm = function (){
	let filename=inputDialogInput.value;
	
	if(filename==="") return;
	let data={
		action:"mkdir",
		source:nowPath+"/"+filename
	}
	doFileCMD(true,data,function(error){
		//失败
		console.log(error)
	},function(res){
		//成功
		console.log(res)
		loadList(nowPath);
	});
}
/******************粘贴文件******************/
var pasteFilesConfirm = function (){
	let paths=[];
	for (let i in selFileIds)
		paths.push(clipboard.dir+"/"+fileVue.fileList[selFileIds[i]].filename)
	let data;
	if(paths.length==1){//单个
		data = {
			action:clipboard.isCopy?'copy':'move',
			source:paths[0],
			target:nowPath
		}
	}else {
		data={
			action:clipboard.isCopy?'copy':'move',
			sources:paths,
			target:nowPath
		}
	}
	//发送请求后刷新啊，暂时模拟
	doFileCMD(paths.length==1,data,function(error){
		//失败
		console.log(error)
	},function(res){
		//成功
		console.log(res)
		loadList(nowPath);
	});
}
/******************删除文件******************/
//弹窗询问
var delFilesConfirm = function () {
		//删除单个文件操作
	let paths=[];
	for (let i in selFileIds)
		paths.push(nowPath+"/"+fileVue.fileList[selFileIds[i]].filename)
	let data;
	if(paths.length==1){
		  data = {
			action:'delete',
			source:paths[0]
		  }
	}else data={
		action:'delete',
		sources:paths
	}
	//发送请求后刷新啊，暂时模拟
	doFileCMD(paths.length==1,data,function(error){
		//失败
		console.log(error)
	},function(res){
		//成功
		console.log(res)
		loadList(nowPath);
	});
};

//确认删除文件

/******************双击文件******************/
//双击文件
function selFileClick(id) {
    let file=fileVue.fileList[id];
    console.log(id)
    console.log(file)
    if (file.isFile){
        console.log("选择的是文件");
    }else{
        if(nowPath=="/")loadList(nowPath+fileVue.fileList[id].filename)
        else loadList(nowPath+"/"+fileVue.fileList[id].filename)
    }
}
//上一级
function loadBack() {
    let names=nowPath.split("/");
    let path="";
    for (let i =1;i<names.length-1;++i)
        path+="/"+names[i];
    if (path==='') path='/';
    loadList(path)
}
/******************文件操作按钮******************/
function fileOpBtClickById(action,id){
	selFileIds=[id];//标记
	console.log(id)
	switch(action){
		case "download": 
			
			break;
		case "share":
			$('#sharedFileLimitDialog').modal('show');
			break;
		case "delete":
			showFileDialog("确认删除以下文件？",delFilesConfirm);
			break;
		case "rename":
			inputDialogTitle.innerHTML="重命名";
			inputDialogInput.value=fileVue.fileList[id].filename;
			inputDialogConfirmBt.onclick=renameFileConfirm;
			$("#inputDialog").modal("show");;
			break;
		case "mkdir":
			inputDialogTitle.innerHTML="新建文件夹";
			inputDialogInput.value=""
			inputDialogConfirmBt.onclick=mkdirConfirm;
			$("#inputDialog").modal("show");;
			break;
		
	}
}
//文件操作按钮
function fileOpBtClick(action){
	console.log(action);
    let checkFileBoxs=document.getElementsByClassName('checkFileBox');
    selFileIds=[];//被选择的文件
    for (let i =0;i<checkFileBoxs.length;++i){
        if (checkFileBoxs[i].checked)
            selFileIds.push(i);
    }
	//文件操作
	console.log("文件操作")
	switch(action){
		case "download": 
			break;
		case "copy":
			clipboard.ids = selFileIds;
			clipboard.used = true;
			clipboard.isCopy = true;
			fileTabBtsVue.clipboardUsed=true;
			break;
		case "cut":
			clipboard.ids = selFileIds;
			clipboard.used = true;
			clipboard.isCopy = false;
			fileTabBtsVue.clipboardUsed=true;
			break;
		case "paste":
			if(clipboard.isCopy){
			showFileDialog("确认将以下文件复制到当前目录？",pasteFilesConfirm);
			}else{
			showFileDialog("确认将以下文件移动到当前目录？",pasteFilesConfirm);
				clipboard.used = false;
			} 
			
			break;
		case "share":
			$('#sharedFileLimitDialog').modal('show');
			break;
		case "delete":
			showFileDialog("确认删除以下文件？",delFilesConfirm);
			break;
	}
}
function removeFile(id) {
    let filePath=nowPath+"/"+fileVue.fileList[id].filename;
    console.log("删除文件 "+filePath);
    $.ajax({
        type: "GET", //提交的方法
        url: "disk/file?action=delete&path="+Base64.encodeURI(filePath), //提交的地址
        //data: $('#loginForm').serialize(), // 序列化表单值
        dataType:'json',
        async: false,
        error: function(request) {
            //失败提示
            console.log(request)
            showError("失败")
        },
        success: function(data) { //成功
            showError("删除成功");
            console.log(data);
            if (data.status===0) {
                loadList(nowPath);
            }
            else showError("获取失败");
        }
    });
}